package edu.gyc.collectionhomework;

import java.util.Objects;
import java.util.TreeSet;

public class Mountain implements Comparable<Mountain>{
        @Override
        public int compareTo(Mountain o) {
            int n=o.height-this.height; //海拔高度是排序的首选条件
            return n == 0 ? this.name.compareTo(o.name):n;//名字是次要条件
        }
    private String name;
    private int height;

    public Mountain(String name, int height) {
        this.name = name;
        this.height = height;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Mountain mountain = (Mountain) o;
        return height == mountain.height &&
                Objects.equals(name, mountain.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, height);
    }

    @Override
    public String toString() {
        return "Mountain{" +
                "name='" + name + '\'' +
                ", height=" + height +
                '}';
    }

    public static void main(String[] args) {
        Mountain taisan = new Mountain("泰山", 1000);
        Mountain huasan = new Mountain("华山", 3000);
        Mountain southhensan = new Mountain("衡山", 1800);
        Mountain northhensan = new Mountain("恒山", 2000);
        Mountain zongsan = new Mountain("嵩山", 1600);

        TreeSet<Mountain> mountains = new TreeSet<>();
        mountains.add(taisan);
        mountains.add(huasan);
        mountains.add(southhensan);
        mountains.add(northhensan);
        mountains.add(zongsan);
        System.out.println(mountains);
    }
}
